package org.jwcloud.project.module.zx.projectFollow.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jwcloud.project.module.zx.common.FmtUtil;
import org.jwcloud.project.module.zx.common.Resp;
import org.jwcloud.project.module.zx.common.repository.RptCommonRepository;
import org.jwcloud.project.module.zx.common.service.RptUtService;
import org.jwcloud.project.module.zx.projectFollow.service.repository.ProjectFollowRepository;
import org.jwcloud.project.module.zx.template.service.repository.RptTemplateRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.jw.mid.mm.comm.common.controller.BaseController;
import com.jw.mid.mm.comm.common.json.Response;
import com.jw.mid.mm.comm.common.pagination.ListBounds;
import com.jw.mid.mm.comm.common.pagination.ListSlice;
import com.jw.mid.mm.comm.common.pagination.PageListSlice;
import com.jw.mid.mm.comm.common.pagination.Pagination;

@Service
@Transactional
public class ProjectFollowServiceImpl extends BaseController implements ProjectFollowService{
	
//	@Autowired
//    public RptCommonRepository rptCommonRepository;
	 
	@Autowired
    public ProjectFollowRepository projectFollowRepository;
	
	@Autowired
    public RptUtService rptUtService;

	@Override
	public Resp<Object> queryList(Pagination pagination, Map param) {
		// TODO Auto-generated method stub
		FmtUtil.pagiBuild(pagination, param);
		
		return Resp.success(this.rptUtService.findPagi(
	    		pagination,
	    		param,
	    		this.projectFollowRepository.queryList(param), 
	    		this.projectFollowRepository.queryList_total(param)
	    	));
		
	}

	@Override
	public Resp<Object> queryFollow(Map param) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> result = projectFollowRepository.queryFollow(param);
		
		boolean flag_warningType = false;
		boolean flag_isReplace = false;
		
		if(result == null || result.size() == 0) {
			return Resp.error("项目数据为空");
		}
		
		for(Map<String, Object> map: result) {
			if(map.get("task_name") == null) {
				continue;
			}
			String task_name = (String)map.get("task_name");
			String task_go_type = (String)map.get("task_go_type");
			String pro_task_info_id = (String)map.get("pro_task_info_id");
			String max_is_mvp = (String)map.get("max_is_mvp");
			String warning_type = (String)map.get("warning_type");
			String max_is_replace = (String)map.get("max_is_replace");
			String rel_task_id = (String)map.get("rel_task_id");
			String sp_num = (String)map.get("sp_num");
			String taskStartDate = (String)map.get("taskStartDate");
			String taskDoneDate = (String)map.get("taskDoneDate");
			String is_man_tip_done = (String)map.get("is_man_tip_done");
			String is_done = (String)map.get("is_done");
			
			
			
			List<Map<String, Object>> tempList = new ArrayList();
			
			if(!StringUtils.isEmpty(task_name)) {
				String[] task_names = task_name.split(",");
				String[] task_go_types = task_go_type.split(",");
				String[] pro_task_info_ids = pro_task_info_id.split(",");
				String[] max_is_mvps = max_is_mvp.split(",");
				String[] warning_types = warning_type.split(",");
				String[] max_is_replaces = max_is_replace.split(",");
				String[] rel_task_ids = rel_task_id.split(",");
				String[] sp_nums = sp_num.split(",");
				
				String[] taskStartDates = taskStartDate.split(",");
				String[] taskDoneDates = taskDoneDate.split(",");
				
				String[] is_man_tip_dones = is_man_tip_done.split(",");
				String[] is_dones = is_done.split(",");
				
//				System.out.println("max_is_mvps： " + max_is_mvp);
//				System.out.println("max_is_mvps： " + max_is_mvps);
//				if(task_names.length != max_is_mvps.length) {
//					System.out.println("~~~~~~~~~~~~~");
//					System.out.println(task_name);
//					System.out.println(max_is_mvp);
//				}
				for(int i = 0; i < task_names.length; i++) {
					Map newMap = new HashMap();
					newMap.put("this_task_name", task_names[i]);
					newMap.put("this_task_go_types", task_go_types[i]);
					newMap.put("this_pro_task_info_id", pro_task_info_ids[i]);
					newMap.put("this_max_is_mvp", max_is_mvps[i]);
					newMap.put("this_warning_type", warning_types[i]);//1有未完成预警
					newMap.put("this_max_is_replace", max_is_replaces[i]);//1有替换要件
					newMap.put("taskPzId", rel_task_ids[i]);
					newMap.put("needApproved", "0".equals(sp_nums[i]) ? "0" : "1");
					
					newMap.put("taskStartDate", taskStartDates[i]);
					newMap.put("taskDoneDate", taskDoneDates[i]);
					
					newMap.put("is_man_tip_done", is_man_tip_dones[i]);
					newMap.put("is_done", is_dones[i]);
					
					Map innerMap = new HashMap();
					innerMap.put("relWorkId", ""+map.get("work_id"));
					newMap.put("rtaProTaskInfo", innerMap);
//					newMap.put("relWorkId", ""+map.get("work_id"));
					
//					if(sp_nums.length != task_names.length) {
//						System.out.println("~~~~~~~~~~~~~~~~~~~~");
//						System.out.println(sp_num + " " + task_name);
//					}
					
					tempList.add(newMap);
					
					if("1".equals(warning_types[i])) {
						flag_warningType = true;
					}
					if("1".equals(max_is_replaces[i])) {
						flag_isReplace = true;
					}
					
				}
				map.put("task_list", tempList);
				if(flag_warningType) {
					map.put("this_work_warning_type", "1");
				}else {
					map.put("this_work_warning_type", "0");
				}
				if(flag_isReplace) {
					map.put("this_work_is_replace", "1");
				}else {
					map.put("this_work_is_replace", "0");
				}
				flag_warningType = false;
				flag_isReplace = false;
			}
		}
		
		System.out.println(result);
		
		List<Map<String, Object>> trueList = new ArrayList();
		List<Map<String, Object>> workList = new ArrayList();//阶段对应的worklist
		Map tempWorkMap = new HashMap();
		String app_phase = "";
		int i = 0;
		if(result != null && result.size() > 0) {
			app_phase = (String) result.get(0).get("app_phase_desc");
			for(i = 0; i < result.size(); ) {
				if(app_phase.equals((String) result.get(i).get("app_phase_desc"))) {
					workList.add(result.get(i));
				}else {
					tempWorkMap = new HashMap();
					tempWorkMap.put("name", app_phase);
					tempWorkMap.put("list", workList);
					trueList.add(tempWorkMap);
					workList = new ArrayList();
					
					app_phase = (String) result.get(i).get("app_phase_desc");
					continue;
				}
				app_phase = (String) result.get(i).get("app_phase_desc");
				i++;
			}
		}
		tempWorkMap = new HashMap();
		if(workList.size() == 0 && (i - 1) < result.size()) {
			workList.add(result.get(i - 1));
		}
		tempWorkMap.put("name", app_phase);
		tempWorkMap.put("list", workList);
		trueList.add(tempWorkMap);
		
		return Resp.success(trueList);
	}
	

}
